﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FimetContoDeposito.DAL;
using GrayParrot.Core;
using Test.DAL;

namespace FimetContoDeposito.BLL.Facades
{
    public class CustomersFacade : FacadeBase<CustomersFacade>
    {
        NorthWindContext m_ctx = null;

        public Repository<Customer, string> Repo { get; internal set; }

        public CustomersFacade()
        {
        }

        public override void Initialize()
        {
            m_ctx = new NorthWindContext();
            m_ctx.Configuration.LazyLoadingEnabled = false;
            m_ctx.Configuration.ProxyCreationEnabled = false;

            Repo = this.GetRepository<Customer, string>(m_ctx);
        }

        public override void Dispose()
        {
            if (m_ctx != null)
            {
                m_ctx.Dispose();

                Repo.Dispose();
            }
        }

        public ReturnValue<bool> Add(Customer item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                if (item.CustomerId == string.Empty)
                {
                    item.CustomerId = Guid.NewGuid().ToString().Substring(1, 5);
                }

                this.Repo.Add(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Edit(Customer item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Edit(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Delete(Customer item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Delete(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<Customer> Get(string id)
        {
            ReturnValue<Customer> retValue = new ReturnValue<Customer>();

            try
            {
                retValue.Value = this.Repo.Get(id);
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<Customer>> GetAll()
        {
            ReturnValue<IQueryable<Customer>> retValue = new ReturnValue<IQueryable<Customer>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                retValue.Value = this.Repo.GetAll();
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<List<string>> GetCompanies()
        {
            ReturnValue<List<string>> retValue = new ReturnValue<List<string>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                var objs = (from x in m_ctx.Customers
                            select x.CompanyName).Distinct();

                if (objs.Any())
                {
                    retValue.Value = objs.ToList();
                    retValue.HasValue = true;
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<List<NameValue<string, string>>> GetRegions()
        {
            ReturnValue<List<NameValue<string, string>>> retValue = new ReturnValue<List<NameValue<string, string>>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                var objs = from x in m_ctx.Regions
                           select new NameValue<string, string>() { name=x.RegionDescription, value=x.RegionId.ToString() };

                if (objs.Any())
                {
                    retValue.Value = objs.ToList();
                    retValue.HasValue = true;
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }
    }
}
